package com.example.administrator.webandjs;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.web);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new MyObj(), "myObj");
        webView.loadUrl("file:///android_asset/index.html");
    }

    public class MyObj {
        //此方法实现点击网页实现安卓原生的改变
        @JavascriptInterface
        public void setValuess(String name) {
            Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();

        }
    }

    public void btn(View view) {
//这一处实现点击安卓原生控件，实现网页内容的变化
            webView.loadUrl("javascript:imgFun()");
        }

}
